<template>
  <div class="main">
    <a-form
      id="formLogin"
      class="user-layout-login"
      ref="formLogin"
      :form="form"
      @submit="handleSubmit"
    >
      <a-tabs
        :activeKey="customActiveKey"
        :tabBarStyle="{ textAlign: 'center', borderBottom: 'unset' }"
        @change="handleTabClick"
      >
        <a-tab-pane key="tab1" tab="登录">
          <a-alert v-if="isLoginError" type="error" showIcon style="margin-bottom: 24px;" :message="$t('user.login.message-invalid-credentials')" />
          <a-form-item>
            <a-input
              size="large"
              type="text"
              placeholder="请输入帐号"
              v-decorator="[
                'username',
                {rules: [{ required: true, message: '请输入账号' }, { min:1, max:30, message: '请输入1~30位的账号' }], validateTrigger: 'change'}
              ]"
            >
              <a-icon slot="prefix" type="user" :style="{ color: 'rgba(0,0,0,.25)' }"/>
            </a-input>
          </a-form-item>

          <a-form-item>
            <a-input-password
              size="large"
              placeholder="请输入密码"
              v-decorator="[
                'password',
                {rules: [{ required: true, message: '请输入密码' }], validateTrigger: 'blur'}
              ]"
            >
              <a-icon slot="prefix" type="lock" :style="{ color: 'rgba(0,0,0,.25)' }"/>
            </a-input-password>
          </a-form-item>
        </a-tab-pane>
      </a-tabs>
      <a-form-item style="margin-top:24px">
        <a-button
          size="large"
          type="primary"
          htmlType="submit"
          class="login-button"
          :loading="state.loginBtn"
          :disabled="state.loginBtn"
        >登录</a-button>
      </a-form-item>
    </a-form>
  </div>
</template>

<script>
import { mapActions } from 'vuex'
import { timeFix } from '@/utils/util'

export default {
  data () {
    return {
      customActiveKey: 'tab1',
      loginBtn: false,
      // login type: 0 email, 1 username, 2 telephone
      loginType: 0,
      isLoginError: false,
      form: this.$form.createForm(this),
      state: {
        time: 60,
        loginBtn: false,
        // login type: 0 email, 1 username, 2 telephone
        loginType: 0,
        smsSendBtn: false
      }
    }
  },
  created () {
  
  },
  methods: {
    ...mapActions(['Login', 'Logout', 'GetConfig']),
    // handler
    validatorUsername (rule, value, callback) {
      
      callback()
    },
    handleTabClick (key) {
      this.customActiveKey = key
      // this.form.resetFields()
    },
    handleSubmit (e) {
      e.preventDefault()
      e.preventDefault()
      // const {
      //   form: { validateFields },
      //   state,
      //   customActiveKey,
      //   Login
      // } = this
      
      // state.loginBtn = true
      // const page = ["ydiandajifenrelease1","ydiandajifen1","ydiandajifenrule1","ydiandasystem1","ydiandasyusers1","ydiandasyrole1","ydiandasydepartment1","ydiandasylog1","ydiandasydictionary1","ydiandajiaowuzhongxin1","ydiandataskCignup1","ydiandataskCurriculum1","ydiandataskCourseClassification1","ydiandataskXiaoqu1","ydiandataskTeacher1","ydiandatask1","ydiandatasktaskset1","ydiandatasktaskList1","ydiandames1","ydiandamesrelease1","ydiandamesclassification1","ydiandacontribution1","ydiandacontributionindex1","ydiandacontrirule1","ydiandamessage1","ydiandamessages1","ydiandaperson1","ydiandastudent1","ydiandateacher1","ydiandavolunteer1","ydiandacomment1"]
      // const role = ["jifenrelease_add2","rules_add2","rules_edit2","rules_del2","users_add2","users_edit2","users_editPsa2","users_del2","role_add2","role_edit2","role_del2","department_add2","department_edit2","department_del2","department_status2","Cignup_add2","Cignup_edit2","Cignup_del2","Cignup_watch2","Cignup_status2","Curriculum_add2","Curriculum_edit2","Curriculum_del2","Curriculum_watch2","Curriculum_mulu2","Classification_add2","Classification_edit2","Classification_del2","Xiaoqu_add2","Xiaoqu_edit2","Xiaoqu_del2","Teacher_add2","Teacher_edit2","Teacher_del2","Teacher_status2","taskset_add2","taskset_edit2","taskset_status2","taskset_del2","release_add2","release_edit2","release_del2","release_open2","release_code2","classification_add2","classification_edit2","classification_del2","ydiandacontributionindex_add2","ydiandacontributionindex_watch2","contrirule_add2","contrirule_edit2","contrirule_del2","messages_add2","messages_edit2","messages_del2"]
      // localStorage.setItem('users_id',41);
      // localStorage.setItem('username','admin');
      // localStorage.setItem('token','VJaeZ2/c12xpY5u9VpWo0c+mm6fCoKTAWWycnWFtk5ZoZppmapmfpXJqm71WpaPG2JOfmL9Zcc1xZWSerVk=');
      // localStorage.setItem('role',JSON.stringify(role));
      // localStorage.setItem('page',JSON.stringify(page));
      // localStorage.setItem('role_type',2);
      // this.loginSuccess()
      const {
        form: { validateFields },
        state,
        customActiveKey,
        Login
      } = this

      state.loginBtn = true
      const validateFieldsKey = customActiveKey === 'tab1' ? ['username', 'password'] : ['mobile', 'captcha']
      validateFields(validateFieldsKey, { force: true }, (err, values) => {
        if (!err) {
          const loginParams = { ...values }
          Login(loginParams)
            .then((res) => {
              if( res ){
                localStorage.setItem('users_id',res.data.user_id);
                localStorage.setItem('username',res.data.username);
                localStorage.setItem('token',res.data.token);
                const page = ["ydiandajifenrelease1","ydiandajifen1","ydiandajifenrule1","ydiandasystem1","ydiandasyusers1","ydiandasyrole1","ydiandasydepartment1","ydiandasylog1","ydiandasydictionary1","ydiandajiaowuzhongxin1","ydiandataskCignup1","ydiandataskCurriculum1","ydiandataskCourseClassification1","ydiandataskXiaoqu1","ydiandataskTeacher1","ydiandatask1","ydiandatasktaskset1","ydiandatasktaskList1","ydiandames1","ydiandamesrelease1","ydiandamesclassification1","ydiandacontribution1","ydiandacontributionindex1","ydiandacontrirule1","ydiandamessage1","ydiandamessages1","ydiandaperson1","ydiandastudent1","ydiandateacher1","ydiandavolunteer1","ydiandacomment1"]
                const role = ["jifenrelease_add2","rules_add2","rules_edit2","rules_del2","users_add2","users_edit2","users_editPsa2","users_del2","role_add2","role_edit2","role_del2","department_add2","department_edit2","department_del2","department_status2","Cignup_add2","Cignup_edit2","Cignup_del2","Cignup_watch2","Cignup_status2","Curriculum_add2","Curriculum_edit2","Curriculum_del2","Curriculum_watch2","Curriculum_mulu2","Classification_add2","Classification_edit2","Classification_del2","Xiaoqu_add2","Xiaoqu_edit2","Xiaoqu_del2","Teacher_add2","Teacher_edit2","Teacher_del2","Teacher_status2","taskset_add2","taskset_edit2","taskset_status2","taskset_del2","release_add2","release_edit2","release_del2","release_open2","release_code2","classification_add2","classification_edit2","classification_del2","ydiandacontributionindex_add2","ydiandacontributionindex_watch2","contrirule_add2","contrirule_edit2","contrirule_del2","messages_add2","messages_edit2","messages_del2"]
                localStorage.setItem('role',JSON.stringify(role));
                localStorage.setItem('page',JSON.stringify(page));
                this.loginSuccess(res)
              }
            })
            .catch(error => {
              this.$notification.info({
                message: '错误',
                description: `账号密码错误`
              })
            })
            .finally(() => {
              console.log(1);
              state.loginBtn = false
            })
        } else {
          setTimeout(() => {
            state.loginBtn = false
          }, 600)
        }
      })
    },
    loginSuccess (res) {
      console.log("login")
      this.$router.push('/')
      // 延迟 1 秒显示欢迎信息
      setTimeout(() => {
        this.$notification.success({
          message: '欢迎',
          description: `${timeFix()}，欢迎回来`
        })
      }, 1000)
      this.isLoginError = false
    },
  }
}
</script>

<style lang="less" scoped>
.user-layout-login {
  label {
    font-size: 14px;
  }

  .getCaptcha {
    display: block;
    width: 100%;
    height: 40px;
  }

  .forge-password {
    font-size: 14px;
  }

  button.login-button {
    padding: 0 15px;
    font-size: 16px;
    height: 40px;
    width: 100%;
  }

  .user-login-other {
    text-align: left;
    margin-top: 24px;
    line-height: 22px;

    .item-icon {
      font-size: 24px;
      color: rgba(0, 0, 0, 0.2);
      margin-left: 16px;
      vertical-align: middle;
      cursor: pointer;
      transition: color 0.3s;

      &:hover {
        color: #1890ff;
      }
    }

    .register {
      float: right;
    }
  }
}
</style>
